<template>
  <div class="wrapper">
    <!-- header部分 -->
    <header>
      <BackButton></BackButton>
      <p>地址管理</p>
    </header>
    <!-- 地址列表部分 -->
    <ul class="addresslist">
      <li v-for="item in deliveryAddressArr" :key="item.daId">
        <div class="addresslist-left" @click="setDeliveryAddress(item)">
          <h3>
            {{ item.contactName }}{{ sexFilter_filter(item.contactSex) }}
            {{ item.contactTel }}
          </h3>
          <p>{{ item.address }}</p>
        </div>
        <div class="addresslist-right">
          <i class="fa fa-edit" @click="editUserAddress(item.daId)"></i>
          <i class="fa fa-remove" @click="removeUserAddress(item.daId)"></i>
        </div>
      </li>
    </ul>
    <!-- 新增地址部分 -->
    <div class="addbtn" @click="toAddUserAddress">
      <i class="fa fa-plus-circle"></i>
      <p>新增收货地址</p>
    </div>
    <!-- 底部菜单部分 -->
    <Footer></Footer>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import Footer from '../components/Footer.vue';
import BackButton from '@/components/backButton.vue';
import axios from 'axios';

const route = useRoute();
const router = useRouter();
const businessId = ref(route.query.businessId);
const user = ref({});
const deliveryAddressArr = ref([]);

onMounted(() => {
  user.value = JSON.parse(sessionStorage.getItem('user'));
  listDeliveryAddressByUserId();
});

const sexFilter_filter = (value) => {
  return value == 1 ? '先生' : '女士';
};

const listDeliveryAddressByUserId = async () => {
  try {
    const response = await axios.get(`/deliveryAddresses/userId/${user.value.userId}`);
    deliveryAddressArr.value = response.data.data;
  } catch (error) {
    console.error(error);
  }
};

const setDeliveryAddress = (deliveryAddress) => {
  sessionStorage.setItem(user.value.userId, JSON.stringify(deliveryAddress));
  router.back();
};

const toAddUserAddress = () => {
  router.push({
    path: '/addUserAddress',
    query: { businessId: businessId.value },
  });
};

const editUserAddress = (daId) => {
  router.push({
    path: '/editUserAddress',
    query: { businessId: businessId.value, daId },
  });
};

const removeUserAddress = async (daId) => {
  if (!confirm('确认要删除此送货地址吗？')) {
    return;
  }

  try {
    const response = await axios.delete(`/deliveryAddresses/${daId}`);
    if (response.data.state > 0) {
      const deliveryAddress = JSON.parse(sessionStorage.getItem(user.value.userId));
      if (deliveryAddress && deliveryAddress.daId == daId) {
        sessionStorage.removeItem(user.value.userId);
      }
      listDeliveryAddressByUserId();
    } else {
      alert('删除地址失败！');
    }
  } catch (error) {
    console.error(error);
  }
};
</script>

<style scoped>
.wrapper {
  width: 100%;
  height: 100%;
  background-color: #f5f5f5;
}
.wrapper header {
  width: 100%;
  height: 12vw;
  background-color: #0097ff;
  display: flex;
  justify-content: space-around;
  align-items: center;
  color: #fff;
  font-size: 4.8vw;
  position: fixed;
  left: 0;
  top: 0;
  z-index: 1000;
}
.wrapper .addresslist {
  width: 100%;
  margin-top: 12vw;
  background-color: #fff;
}
.wrapper .addresslist li {
  width: 100%;
  box-sizing: border-box;
  border-bottom: solid 1px #ddd;
  padding: 3vw;
  display: flex;
}
.wrapper .addresslist li .addresslist-left {
  flex: 5;
  user-select: none;
  cursor: pointer;
}
.wrapper .addresslist li .addresslist-left h3 {
  font-size: 4.6vw;
  font-weight: 300;
  color: #666;
}
.wrapper .addresslist li .addresslist-left p {
  font-size: 4vw;
  color: #666;
}
.wrapper .addresslist li .addresslist-right {
  flex: 1;
  font-size: 5.6vw;
  color: #999;
  cursor: pointer;
  display: flex;
  justify-content: space-around;
  align-items: center;
}
.wrapper .addbtn {
  width: 100%;
  height: 14vw;
  border-top: solid 1px #ddd;
  border-bottom: solid 1px #ddd;
  background-color: #fff;
  margin-top: 4vw;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 4.5vw;
  color: #0097ff;
  user-select: none;
  cursor: pointer;
}
.wrapper .addbtn p {
  margin-left: 2vw;
}
</style>
